package table_system

import (
	"time"

	"gitee.com/klein-x/biz/model/mysql/table"
	"gitee.com/klein-x/biz/module/db/mysql"
)

// HomePage 存储首页信息
type Home struct {
	ID           string    `gorm:"primaryKey;type:char(36);unique;not null;comment:'首页信息ID'"`
	Name         string    `gorm:"type:varchar(255);not null;comment:'首页名称'"`
	GoodsList    []string  `gorm:"type:text;serializer:json;not null;comment:'商品列表'"`
	BannerImages []string  `gorm:"type:text;serializer:json;not null;comment:'Banner图UUID列表'"`
	CreatedAt    time.Time `gorm:"comment:'创建时间'"`
	UpdatedAt    time.Time `gorm:"comment:'更新时间'"`
}

func (hp *Home) TableName() string {
	return "home_page"
}

func HomeGetByAll() (*[]Home, error) {
	db := mysql.GetGlobalDB()

	var home []Home
	return table.SelectCheck(&home, db.Find(&home))
}

func BannerGetByKey(key string) ([]string, error) {
	db := mysql.GetGlobalDB()

	var home Home
	homeptr, err := table.SelectCheck(&home, db.Where("id=?", key).First(&home))
	if err != nil {
		return nil, err
	}

	if homeptr == nil {
		return nil, nil
	}
	return homeptr.BannerImages, nil
	// var l []string
	// json.Unmarshal(, &l)
	// return &l, nil
}

// GetGoodsListByID 获取指定ID的HomePage记录中的GoodsList字段
func GoodsGetByID(key string) ([]string, error) {
	db := mysql.GetGlobalDB()

	var home Home
	homeptr, err := table.SelectCheck(&home, db.Where("id=?", key).First(&home))
	if err != nil {
		return nil, err
	}

	if homeptr == nil {
		return nil, nil
	}
	return homeptr.GoodsList, nil
	// var l []string
	// json.Unmarshal(homeptr.GoodsList, &l)
	// return &l, nil
}
